package com.business.frame.service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import com.business.base.BaseDao;
import com.business.base.Pager;
import com.business.frame.po.TypeBean;
import com.common.NumberProcess;
import com.common.StringProcess;
import com.system.database.DS;

@Service("typeService")
@Transactional
public class TypeService {

	private final String dbName = "frameDb";

	@Resource
	private BaseDao baseDao;
	
	private TypeBean convert(Map<String, Object> model)
	{
		TypeBean bean=new TypeBean();
		
		bean.setTypeId(StringProcess.convertToString(model.get("TypeId")));
		bean.setFieldType(StringProcess.convertToString(model.get("FieldType")));
		bean.setFieldLength(NumberProcess.convertToNumber(model.get("FieldLength")));
		bean.setIsKey(NumberProcess.convertToNumber(model.get("IsKey")));
		bean.setIsAllowNull(NumberProcess.convertToNumber(model.get("IsAllowNull")));
		bean.setTypeMsAccess(StringProcess.convertToString(model.get("TypeMsAccess")));
		bean.setTypeMsSQL(StringProcess.convertToString(model.get("TypeMsSQL")));
		bean.setTypeMySQL(StringProcess.convertToString(model.get("TypeMySQL")));
		bean.setTypeOracle(StringProcess.convertToString(model.get("TypeOracle")));
		bean.setType1(StringProcess.convertToString(model.get("Type1")));
		bean.setType2(StringProcess.convertToString(model.get("Type2")));
		bean.setType3(StringProcess.convertToString(model.get("Type3")));
		bean.setType4(StringProcess.convertToString(model.get("Type4")));
		bean.setIsEnable(NumberProcess.convertToNumber(model.get("IsEnable")));
		bean.setOrderNumber(NumberProcess.convertToNumber(model.get("OrderNumber")));
		
		return bean;
	}

	@DS(dbName)
	public String insert(Map<String, Object> model) {
		baseDao.save("frame.type.insert", model);

		return "";
	}

	@DS(dbName)
	public String delete(String typeId) {
		Map<String, Object> search = new HashMap<String, Object>();
		search.put("typeId", typeId);

		baseDao.save("frame.type.delete", search);

		return "";
	}

	@DS(dbName)
	public String update(Map<String, Object> model) {
		baseDao.save("frame.type.update", model);

		return "";
	}

	@DS(dbName)
	public Pager<Map<String, Object>> selectPage(int pageIndex, int pageSize, Map<String, Object> search) {
		
		Pager<Map<String, Object>> result = baseDao.selectListByPage(pageIndex, pageSize, "frame.type.select",search);
		
		for (int i = 0; i < result.getList().size(); i++) {
			Map<String, Object> item = result.getList().get(i);
			item.put("IndexNumber", (pageIndex - 1) * pageSize + i + 1);
		}

		return result;
	}

	@DS(dbName)
	public List<Map<String, Object>> select(Map<String, Object> search) {
		return baseDao.selectList("frame.type.select", search);
	}
	
	@DS(dbName)
	public TypeBean getBeanByKeyId(String typeId)
	{
		Map<String, Object> search = new HashMap<String, Object>();
		search.put("typeId", typeId);
		List<Map<String, Object>> list=select(search);
		
		TypeBean bean=null;
		if(list.size()>0)
		{
			bean=convert(list.get(0));
		}
		
		return bean;
	}
	
}
